/**
 * @Description: status 指令
 * @author Beon
 * @date 2021/1/4
*/
import * as childProcess from 'child_process';
import {ErrorCatch} from '../decorators/errorCatch';
import '../utils/consoleColor';

export class Status{

    /**
     * 判断暂存区是否添加了文件
     * @param {String} repoPath 执行路径
     * @returns {Promise<void>}
     */
    @ErrorCatch.errorExit('查询缓存区失败，请查看报错原因')
    static async anythingAdd (repoPath?: string): Promise<Buffer> {
        return childProcess.execSync('git status -s', { cwd: repoPath });
    }

    /**
     * 如果为空抛出异常
     * @param {String} repoPath 执行路径
     * @returns {Promise<void>}
     */
    @ErrorCatch.errorExit('缓存区没有被添加或者没有文件被修改')
    static async getAddFiles (repoPath?: string): Promise<string[]> {
        const diff = (await this.anythingAdd(repoPath)).toString();

        return diff.split('\n')
            .filter((item: string) => !(/^s*$/.test(item)))
            .reduce((all: string[], item: string) => all.concat(item.split(' ')[item.split(' ').length - 1]), []);
    }
}
